package org.example.demo.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.example.demo.model.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/return")
//@RestController
@Controller
public class ReturnController {
    @RequestMapping("/index")
    public String returnIndex() {
        return "/index.html";  //返回页面里面的数据
    }

    @ResponseBody  //因为上方的注解@Controller是返回视图,就会去找HTML,所以要加@ResponseBody返回数据
    @RequestMapping("/returnDate")
    public String returnDate() {
        return "返回视图需要的数据";
    }

    @ResponseBody  //因为上方的注解@Controller是返回视图,就会去找HTML,所以要加@ResponseBody返回数据
    @RequestMapping("/returnHtml")
    public String returnHtml() {
        return "<h1>返回HTML代码片段<h1>";
    }

    @ResponseBody  //因为上方的注解@Controller是返回视图,就会去找HTML,所以要加@ResponseBody返回数据
    @RequestMapping("/returnJson")
    public Person returnJson() {
        Person person = new Person();  //创建对象
        person.setId(12);
        person.setName("caijun");
        person.setAge(23);
        return person;
    }

    @ResponseBody  //因为上方的注解@Controller是返回视图,就会去找HTML,所以要加@ResponseBody返回数据
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response) {
        response.setStatus(401);  //通常表示没有登录
        return "设置状态码";
    }
}
